//
//  DataBaseAccess.m
//  Wallets Manager
//
//  Created by Nguyen Chi Cong on 9/24/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "DataBaseAccess.h"
#import "folyAppDelegate.h"
#import "Attributes.h"
#import "DataBaseAccess.h"
@implementation DataBaseAccess

#pragma mark - CREATE

+(BOOL)saveData
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    return [context save:&error];
}

+(BOOL)createWalletWithName: (NSString *)name andBalance:(double)balance andDate: (NSDate *)date andDebt:(double)debt andLoan: (double) loan andPword:(NSString*)pword andImage: (NSString*) image andDescrition: (NSString*)desc andCurrency: (id)typeCurrency
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Wallet *newWallet = [NSEntityDescription insertNewObjectForEntityForName:kWallet inManagedObjectContext:context];
    
    newWallet.w_name = name;
    newWallet.w_balance = [NSNumber numberWithDouble:balance];
    newWallet.w_date = date;
    newWallet.w_debt = [NSNumber numberWithDouble:debt];
    newWallet.w_loan = [NSNumber numberWithDouble:loan];
    newWallet.w_pword = pword;
    newWallet.w_image = image;
    newWallet.w_description = desc;
    newWallet.wToCurrency = typeCurrency;
    
    [DataBaseAccess createPlanWithName:kNonePlan inWallet:newWallet];
    
    return [context save:&error];
}

+(BOOL)createWalletWithName: (NSString *)name andBalance:(double)balance andDate: (NSDate *)date andDebt:(double)debt andLoan: (double) loan andPword:(NSString*)pword andImage: (NSString*) image
{
    return [DataBaseAccess createWalletWithName:name andBalance:balance andDate:date andDebt:debt andLoan:loan andPword:pword andImage:image andDescrition:@"" andCurrency:[[DataBaseAccess getAllCurrency] objectAtIndex:0]];
}

+(BOOL)createWalletWithName: (NSString *)name andBalance: (double)balance andDate: (NSDate *)date andPword:(NSString*)pword andImage: (NSString*) image
{
    return [DataBaseAccess createWalletWithName:name andBalance:balance andDate:date andDebt:0.0 andLoan:0.0 andPword:pword andImage:image];
}

+(BOOL)createPlanWithName: (NSString *)name andAmount: (double)amount andStartDate: (NSDate *)startDate andExpectedDate: (NSDate *)expectedDate inWallet: (id)wallet
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Plan *newPlan = [NSEntityDescription insertNewObjectForEntityForName:kPlan inManagedObjectContext:context];
    
    newPlan.p_name = name;
    newPlan.p_amount = [NSNumber numberWithDouble: amount];
    newPlan.p_start_date = startDate;
    newPlan.p_expected_date = expectedDate;
    newPlan.p_completed = [NSNumber numberWithBool:NO];
    newPlan.p_cancel= [NSNumber numberWithBool:NO];
    newPlan.pToWallet = wallet;
    
//    [newPlan setValue:name forKey:kPlanName];
//    [newPlan setValue:[NSNumber numberWithDouble:amount] forKey:kPlanAmount];
//    [newPlan setValue:startDate forKey:kPlanStartDate];
//    [newPlan setValue:expectedDate forKey:kPlanExpectedDate];
//    [newPlan setValue:[NSNumber numberWithBool:NO] forKey:kPlanpCompleted];
//    [newPlan setValue:[NSNumber numberWithBool:NO] forKey:kPlanCancel];
//    [newPlan setValue:wallet forKey:kPlanToWallet];
    
    return [context save:&error];
}

+(BOOL)createPlanWithName: (NSString *)name inWallet: (id)wallet
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Plan *newPlan = [NSEntityDescription insertNewObjectForEntityForName:kPlan inManagedObjectContext:context];
    
    newPlan.p_name = name;
    newPlan.p_completed = [NSNumber numberWithBool:NO];
    newPlan.p_cancel= [NSNumber numberWithBool:NO];
    newPlan.pToWallet = wallet;
        
    return [context save:&error];
}

+ (BOOL) createPlanWithName:(NSString *)name andAmount:(double)amount andstartAmout:(double)startAmount andImage:(NSString*)image inWallet:(id)wallet andIsCompleted : (BOOL)isCompleted
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Plan *newPlan = [NSEntityDescription insertNewObjectForEntityForName:kPlan inManagedObjectContext:context];
    newPlan.p_name = name;
    newPlan.p_amount = [NSNumber numberWithDouble: amount];
    newPlan.p_startamount = [NSNumber numberWithDouble:startAmount];
    newPlan.p_image = image;
    newPlan.p_start_date = [NSDate date];
    newPlan.p_completed = [NSNumber numberWithBool:isCompleted];
    newPlan.pToWallet = wallet;
    return [context save:&error];
}

+(void) initData
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kIncomeType inManagedObjectContext:context];
    NSArray *incomeType = [context executeFetchRequest:request error:&error];
    if([incomeType count] == 0)
    {
        NSArray *listExpenseType = [[NSArray alloc] initWithObjects:
                                    @"Clothing",
                                    @"Entertainment",
                                    @"Family",
                                    @"Food",
                                    @"Friend",
                                    @"Love",
                                    @"Medical",
                                    @"Shopping",
                                    @"Sport",
                                    @"Study",
                                    @"Travel",
                                    @"Other",
                                   nil ];
        NSArray *imagesExT = [[NSArray alloc] initWithObjects:
                              @"icon_13.png",
                              @"icon_19.png",
                              @"family_icon.png",
                              @"icon_2.png",
                              @"friend_icon.png",
                              @"icon_1.png",
                              @"medical_icon.png",
                              @"icon_3.png",
                              @"sport_icon.png",
                              @"icon_4.png",
                              @"travel_icon.png",
                              @"category.png",
                              nil];
        
        NSArray *listIncomeType = [[NSArray alloc] initWithObjects:
                                   @"Business",
                                   @"Gift",
                                   @"Home Fund",
                                   @"House Renting",
                                   @"Interest",
                                   @"Salary",
                                   @"Other",
                                   nil];
        NSArray *imagesIcT = [[NSArray alloc] initWithObjects:
                              @"business.png",
                              @"gift.png",
                              @"homefund.png",
                              @"houserenting.png",
                              @"interest.png",
                              @"salary.png",
                              @"category.png",
                              nil];
        
        for(id et in listExpenseType)
        {
            ExpenseType *newET = [NSEntityDescription insertNewObjectForEntityForName:kExpenseType inManagedObjectContext:context];
            [newET setValue:et forKey:kExpenseTypeName];
            newET.et_image = [imagesExT objectAtIndex:[listExpenseType indexOfObject:et]];
        }
        
        for(id it in listIncomeType)
        {
            IncomeType *newIT = [NSEntityDescription insertNewObjectForEntityForName:kIncomeType inManagedObjectContext:context];
            [newIT setValue:it forKey:kIncomeTypeName];
            newIT.it_image = [imagesIcT objectAtIndex:[listIncomeType indexOfObject:it]];
        }
        
        Currency *cu01 = [NSEntityDescription insertNewObjectForEntityForName:kCurrency inManagedObjectContext:context];
        cu01.c_name = @"VND";
        cu01.c_symbol = @"x1000 VND";
        cu01.c_rate = [NSNumber numberWithDouble:1.f];
        
        Currency *cu02 = [NSEntityDescription insertNewObjectForEntityForName:kCurrency inManagedObjectContext:context];
        cu02.c_name = @"USD";
        cu02.c_symbol = @"$";
        cu02.c_rate = [NSNumber numberWithDouble:1.f];
        
        Currency *cu03 = [NSEntityDescription insertNewObjectForEntityForName:kCurrency inManagedObjectContext:context];
        cu03.c_name = @"Pound";
        cu03.c_symbol = @"Pound";
        cu03.c_rate = [NSNumber numberWithDouble:1.f];
        
        Currency *cu04 = [NSEntityDescription insertNewObjectForEntityForName:kCurrency inManagedObjectContext:context];
        cu04.c_name = @"Euro";
        cu04.c_symbol = @"E";
        cu04.c_rate = [NSNumber numberWithDouble:1.f];
        
        [context save:&error];
    }else
        return;
    
}

+(BOOL)createIncomeWithname: (NSString *)name andAmount: (double) amount andDate: (NSDate *)date andNotes:(NSString *)notes inPlan: (id)plan ofType: (id)type
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Income *newIncome = [NSEntityDescription insertNewObjectForEntityForName:kIncome inManagedObjectContext:context];
    [newIncome setValue:name forKey:kIncomeName];
    [newIncome setValue:[NSNumber numberWithDouble:amount] forKey:kIncomeAmount];
    [newIncome setValue:date forKey:kIncomeDate];
    [newIncome setValue:notes forKey:kIncomeNotes];
    [newIncome setValue:plan forKey:kIncomeToPlan];
    [newIncome setValue:type forKey:kIncomeToIncomeType];
    
    return [context save:&error]; 
}

+(BOOL)createExpenseWithname: (NSString *)name andAmount: (double) amount andDate: (NSDate *)date andNotes:(NSString *)notes inPlan: (id)plan ofType: (id)type
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Expense *newExpense = [NSEntityDescription insertNewObjectForEntityForName:kExpense inManagedObjectContext:context];
    [newExpense setValue:name forKey:kExpenseName];
    [newExpense setValue:[NSNumber numberWithDouble:amount] forKey:kExpenseAmount];
    [newExpense setValue:date forKey:kExpenseDate];
    [newExpense setValue:notes forKey:kExpenseNotes];
    [newExpense setValue:plan forKey:kExpenseToPlan];
    [newExpense setValue:type forKey:kExpenseToExpenseType];
    
    return [context save:&error];
}

+(BOOL)createDebtWithName: (NSString *)name andAmount: (double)amount andDate: (NSDate *)date andExpectedDate: (NSDate *)expectedDate andLender: (NSString *)lender andNotes: (NSString *)notes inWallet: (id)wallet
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    Debt *newDebt = [NSEntityDescription insertNewObjectForEntityForName:kDebt inManagedObjectContext:context];
    
    [newDebt setValue:name forKey:kDebtName];
    [newDebt setValue:[NSNumber numberWithDouble:amount] forKey:kDebtAmount];
    [newDebt setValue:date forKey:kDebtDate];
    [newDebt setValue:expectedDate forKey:kDebtExpectedDate];
    [newDebt setValue:lender forKey:kDebtLender];
    [newDebt setValue:notes forKey:kDebtNotes];
    [newDebt setValue:wallet forKey:kDebtToWallet];
    
    return [context save:&error];
}

+(BOOL)createLoanWithName: (NSString *)name andAmount: (double)amount andDate: (NSDate *)date andExpectedDate: (NSDate *)expectedDate andBorrower: (NSString *)borrower andNotes: (NSString *)notes inWallet: (id)wallet
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSManagedObject *newLoan = [NSEntityDescription insertNewObjectForEntityForName:kLoan inManagedObjectContext:context];
    
    [newLoan setValue:name forKey:kLoanName];
    [newLoan setValue:[NSNumber numberWithDouble:amount] forKey:kLoanAmount];
    [newLoan setValue:date forKey:kLoanDate];
    [newLoan setValue:expectedDate forKey:kLoanExpectedDate];
    [newLoan setValue:borrower forKey:kLoanBorrower];
    [newLoan setValue:notes forKey:kLoanNotes];
    [newLoan setValue:wallet forKey:kLoanToWallet];
    
    return [context save:&error];
}

+(BOOL)createDebtHistory: (double)amount onDate: (NSDate *)date inDebt: (id)debt
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSManagedObject *newDebtHistory = [NSEntityDescription insertNewObjectForEntityForName:kDebtHistory inManagedObjectContext:context];
    [newDebtHistory setValue:[NSNumber numberWithDouble:amount] forKey:kDebtHistoryAmount];
    [newDebtHistory setValue:date forKey:kDebtHistoryDate];
    [newDebtHistory setValue:debt forKey:kDebtHistoryToDebt];
    
    return [context save:&error];

}

+(BOOL)createLoanHistory: (double)amount onDate: (NSDate *)date inLoan: (id)loan
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSManagedObject *newLoanHistory = [NSEntityDescription insertNewObjectForEntityForName:kLoanHistory inManagedObjectContext:context];
    [newLoanHistory setValue:[NSNumber numberWithDouble:amount] forKey:kLoanHistoryAmount];
    [newLoanHistory setValue:date forKey:kLoanHistoryDate];
    [newLoanHistory setValue:loan forKey:kLoanHistoryToLoan];
    
    return [context save:&error];

}

+(BOOL)createExpenseTypeWithName: (NSString *)name andImage: (NSString *)image
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    ExpenseType *newExenpseType = [NSEntityDescription insertNewObjectForEntityForName:kExpenseType inManagedObjectContext:context];
    newExenpseType.et_name = name;
    newExenpseType.et_image = image;
    
    return [context save:&error];
}

+(BOOL)createIncomeTypeWithName: (NSString *)name andImage: (NSString *)image
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    IncomeType *newIncomeType = [NSEntityDescription insertNewObjectForEntityForName:kIncomeType inManagedObjectContext:context];
    newIncomeType.it_name = name;
    newIncomeType.it_image = image;
    
    return [context save:&error];
}

#pragma mark - QUERY
//QUERY
+(NSArray *)getAllWallet
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kWallet inManagedObjectContext:context];
    
    NSArray *listWallet = [context executeFetchRequest:request error:&error];
    
//    Plan *p = [[DataBaseAccess getAllPlanInWallet:[listWallet objectAtIndex:0]] objectAtIndex:0];
//    IncomeType *t = [[DataBaseAccess getAllIncomeType]objectAtIndex:0];
//    
////    for(int i = 0; i<5; i++)
////    {
////        [DataBaseAccess createIncomeWithname:[NSString stringWithFormat:@"Income %i", i] andAmount:(100+i) andDate:[NSDate date] andNotes:@"No comment" inPlan:p ofType:t];
////    }
//    
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
//    
//    NSNumber *sum = [DataBaseAccess numberSumIncomesOfType:t inWallet:[listWallet objectAtIndex:0] inTimeFrom:[dateFormatter dateFromString:@"10/8/2012"] to:[dateFormatter dateFromString:@"10/10/2012"]];
//    NSLog(@"%@", sum);
    
    return listWallet;
}

+(NSArray *)getAllIncomeType
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kIncomeType inManagedObjectContext:context];
    
    NSArray *listIncomeType = [context executeFetchRequest:request error:&error];
    
    return listIncomeType;
}

+(NSArray *)getAllExpenseType
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kExpenseType inManagedObjectContext:context];
    
    NSArray *listExpenseType = [context executeFetchRequest:request error:&error];
    
    return listExpenseType;
}

+(NSArray *)getAllCurrency
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kCurrency inManagedObjectContext:context];
    
    NSArray *listCurrency = [context executeFetchRequest:request error:&error];
    
    return listCurrency;
}

+(NSArray *)getAllPlanInWallet:(id)wallet
{
    NSSet *setOfPlan = ((Wallet *)wallet).wToPlan;
    NSArray *listPlan = [setOfPlan allObjects];
    return listPlan;
}

+(NSArray *)getAllLoanInWallet:(id)wallet
{
    NSSet *setOfLoan = ((Wallet *)wallet).wToLoan;
    NSArray *listLoan = [setOfLoan allObjects];
    return listLoan;
}

+(NSArray *)getAllDebtInWallet:(id)wallet
{
    NSSet *setOfDebt = ((Wallet *)wallet).wToDebt;
    NSArray *listDebt = [setOfDebt allObjects];
    return listDebt;
}

+(NSArray *)getAllIncomeInPlan:(id)plan
{
    NSSet *setOfIncome = ((Plan *)plan).pToIncome;
    NSArray *listIncome = [setOfIncome allObjects];
    return listIncome;

}

+(NSArray *)getAllExpenseInPlan:(id)plan
{
    NSSet *set = ((Plan *)plan).pToExpense;
    NSArray *list = [set allObjects];
    return list;
}

+(NSArray *)getAllIncomeInIncomeType:(id)incomeType
{
    NSSet *set = ((IncomeType *)incomeType).itToIncome;
    NSArray *list = [set allObjects];
    return list;
}

+(NSArray *)getAllExpenseInExpenseType:(id)expenseType
{
    NSSet *set = ((ExpenseType *)expenseType).etToExpense;
    NSArray *list = [set allObjects];
    return list;
}

+(NSArray *)getAllDebtHistoryInDebt:(id)debt
{
    NSSet *set = ((Debt *)debt).dToDebtHistory;
    NSArray *list = [set allObjects];
    return list;
}

+(NSArray *)getAllLoanHistoryInLoan:(id)loan
{
    NSSet *set = ((Loan *)loan).lToLoanHistory;
    NSArray *list = [set allObjects];
    return list;
}

+ (NSArray*) getPlanByStatus : (Boolean)isCompleted andWallet : (Wallet *) wallet
{
    folyAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [delegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    request.entity = [NSEntityDescription entityForName:kPlan inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"(%K = %@) AND (%K = %@)", kPlanToWallet, wallet, kPlanCompleted, [NSNumber numberWithBool:NO]];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return result;
}

+(NSArray *)getAllIncomesInWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to: (NSDate *)dateTo
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kIncome inManagedObjectContext:context];
    //NSLog(@"%lu to %lu", (long)dateFrom, (long)dateTo);

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K < %@)", kIncomeDate, dateFrom, kIncomeDate, dateTo];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kIncomeDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSMutableArray *marrIncome = [NSMutableArray arrayWithArray:[context executeFetchRequest:request error:&error]];
    for(Income *income in marrIncome)
    {
        if(((Plan *)income.iToPlan).pToWallet != wallet)
        {
            [marrIncome removeObject:income];
        }
    }
    
    return marrIncome;

}

+(NSArray *)getAllExpensesInWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to: (NSDate *)dateTo
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kExpense inManagedObjectContext:context];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K < %@)",kExpenseDate, dateFrom, kExpenseDate, dateTo];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kExpenseDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSMutableArray *marrExpenses = [NSMutableArray arrayWithArray:[context executeFetchRequest:request error:&error]];
    for(Expense *expense in marrExpenses)
    {
        if(((Plan *)expense.eToPlan).pToWallet != wallet)
        {
            [marrExpenses removeObject:expense];
        }
    }
    
    return marrExpenses;
}

+(NSArray *)getAllIncomesOfType:(IncomeType *)ict inWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to: (NSDate *)dateTo
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kIncome inManagedObjectContext:context];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K < %@) AND (%K = %@)",kIncomeDate, dateFrom, kIncomeDate, dateTo, kIncomeToIncomeType, ict];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kIncomeDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSMutableArray *marrIncome = [NSMutableArray arrayWithArray:[context executeFetchRequest:request error:&error]];
    for(Income *income in marrIncome)
    {
        if(((Plan *)income.iToPlan).pToWallet != wallet)
        {
            [marrIncome removeObject:income];
        }
    }
    
    return marrIncome;

}

+(NSArray *)getAllExpensesOfType: (ExpenseType *)ett inWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to: (NSDate *)dateTo
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kExpense inManagedObjectContext:context];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K < %@) AND (%K = %@)",kExpenseDate, dateFrom, kExpenseDate, dateTo, kExpenseToExpenseType, ett];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kExpenseDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSMutableArray *marrExpenses = [NSMutableArray arrayWithArray:[context executeFetchRequest:request error:&error]];
    for(Expense *expense in marrExpenses)
    {
        if(((Plan *)expense.eToPlan).pToWallet != wallet)
        {
            [marrExpenses removeObject:expense];
        }
    }
    
    return marrExpenses;
}

+(NSDecimalNumber *)numberSumIncomesOfType:(IncomeType *)ict inWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to: (NSDate *)dateTo
{
    NSArray *arrIncomes = [DataBaseAccess getAllIncomesOfType:ict inWallet:wallet inTimeFrom:dateFrom to:dateTo];
    double sum = 0.0f;
    for(Income *i in arrIncomes)
    {
        sum += [i.i_amount doubleValue];
    }
    NSString *strSum = [NSString stringWithFormat:@"%.2f", sum];
    return [NSDecimalNumber decimalNumberWithString:strSum];
}

+(NSDecimalNumber *)numberSumExpensesOfType:(ExpenseType *)ett inWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to: (NSDate *)dateTo
{
    NSArray *arrExpenses = [DataBaseAccess getAllExpensesOfType:ett inWallet:wallet inTimeFrom:dateFrom to:dateTo];
    double sum = 0.0f;
    for(Expense *e in arrExpenses)
    {
        sum += [e.e_amount doubleValue];
    }
    NSString *strSum = [NSString stringWithFormat:@"%.2f", sum];
    return [NSDecimalNumber decimalNumberWithString:strSum];
}


+(NSArray *)getAllDebtEventsInWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom to:(NSDate *)dateTo
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kDebt inManagedObjectContext:context];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K < %@)",kDebtDate, dateFrom, kDebtDate, dateTo];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kDebtDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSArray *arrDebt = [context executeFetchRequest:request error:&error];
    
    return arrDebt;
    
}

//---------------KODAK------------------
+(NSArray *)getAllDebtEventsInWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom {
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kDebt inManagedObjectContext:context];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@)",kDebtDate, dateFrom];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kDebtDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSArray *arrDebt = [context executeFetchRequest:request error:&error];
    
    return arrDebt;
}

+(NSArray *)getAllLoanEventsInWallet:(id)wallet inTimeFrom:(NSDate *)dateFrom to:(NSDate *)dateTo
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:kLoan inManagedObjectContext:context]];
    NSPredicate  *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K <= %@)",kLoanDate, dateFrom, kLoanDate, dateTo];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kLoanDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSArray *arrLoan = [context executeFetchRequest:request error:&error];
    
    return arrLoan;
}

//---------------KODAK------------------
+(NSArray *)getAllLoanEventsInWallet: (id)wallet inTimeFrom: (NSDate *)dateFrom {
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:kLoan inManagedObjectContext:context]];
    NSPredicate  *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@)",kLoanDate, dateFrom];
    [request setPredicate:predicate];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
                                        initWithKey:kLoanDate ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    NSArray *arrLoan = [context executeFetchRequest:request error:&error];
    
    return arrLoan;
}

+(NSMutableArray*) getAllIncomeDate:(id)wallet {
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSArray *plans = [self getAllPlanInWallet:wallet];
    
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:kIncome inManagedObjectContext:context];
    request.entity = entity;
    request.propertiesToFetch = [NSArray arrayWithObject:[entity.propertiesByName objectForKey:@"i_date"]];
    request.returnsDistinctResults = YES;
    request.resultType = NSDictionaryResultType;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"i_date" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@",kIncomeToPlan,plans];
    [request setPredicate:predicate];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    NSMutableArray *finalDate = [[NSMutableArray alloc]init];
    for(int i = 0 ; i< [result count]; i++) {
        NSDictionary *tempDict = [result objectAtIndex:i];
        [finalDate addObject:[tempDict objectForKey:@"i_date"]];
    }
    return finalDate;
}

+(NSMutableArray*) getAllExpenseDate:(id)wallet {
    
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSArray *plans = [self getAllPlanInWallet:wallet];
    
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:kExpense inManagedObjectContext:context];
    request.entity = entity;
    request.propertiesToFetch = [NSArray arrayWithObject:[entity.propertiesByName objectForKey:@"e_date"]];
    request.returnsDistinctResults = YES;
    request.resultType = NSDictionaryResultType;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"e_date" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K IN %@",kExpenseToPlan,plans];
    [request setPredicate:predicate];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    NSMutableArray *finalDate = [[NSMutableArray alloc]init];
    for(int i = 0 ; i< [result count]; i++) {
        NSDictionary *tempDict = [result objectAtIndex:i];
        [finalDate addObject:[tempDict objectForKey:@"e_date"]];
    }
    return finalDate;
}

+(NSMutableArray*) getAllDate:(id)wallet {
    
    folyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    NSMutableArray *incomeDate = [DataBaseAccess getAllIncomeDate:delegate.currentWallet];
    NSMutableArray *expenseDate = [DataBaseAccess getAllExpenseDate:delegate.currentWallet];
    NSArray *unionDate = [incomeDate arrayByAddingObjectsFromArray:expenseDate];
   
    NSMutableArray *uniqueItems = [NSMutableArray array];
    for (id item in unionDate)
        if (![uniqueItems containsObject:item])
            [uniqueItems addObject:item];
    NSLog(@"%@",uniqueItems);
    return uniqueItems;
}

+(NSDictionary*) getIncomeAndExpenseByDate :(Wallet *)wallet {
    NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
    NSMutableArray *allDates = [self getAllDate:wallet];
    NSMutableArray *incomeArray = [self getAllIncomeByWallet:wallet];
    NSMutableArray *expenseArray = [self getAllExpenseByWallet:wallet];
    
    NSLog(@"income : %d , expense %d",[incomeArray count],[expenseArray count]);
    
    for (NSDate *date in allDates) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"MM/dd/yyyy"];
        NSString *dateString = [dateFormatter stringFromDate:date];
        NSLog(@"%@",dateString);
        
        NSMutableArray *incomeArrayInDate = [[NSMutableArray alloc]init];
        NSMutableArray *expenseArrayInDate = [[NSMutableArray alloc]init];
        
        for(Income *income in incomeArray){
            if (income.i_date.timeIntervalSince1970 == date.timeIntervalSince1970){
                [incomeArrayInDate addObject:income];
            }
        }
        
        for (Expense *expense in expenseArray) {
            if(expense.e_date.timeIntervalSince1970 == date.timeIntervalSince1970) {
                [expenseArrayInDate addObject:expense];
            }
        }
        [tempDict setValue:incomeArrayInDate forKey:dateString];
        [tempDict setValue:expenseArrayInDate forKey:[NSString stringWithFormat:@"expense%@",dateString]];
    }
    return tempDict;
}

+(NSMutableArray*) getAllIncomeByDate:(Wallet *)wallet andDate:(NSDate *)date {
    NSMutableArray *incomeArray = [self getAllIncomeByWallet:wallet];
    NSMutableArray *incomeArrayInDate = [[NSMutableArray alloc]init];
    for(Income *income in incomeArray){
        if (income.i_date.timeIntervalSince1970 == date.timeIntervalSince1970){
            [incomeArrayInDate addObject:income];
        }
    }
    
    return incomeArrayInDate;
}

+(NSMutableArray*) getAllIncomeByDate:(Wallet *)wallet andDateFrom:(NSDate *)dateFrom andDateTo:(NSDate *)dateTo {
    
    NSMutableArray *incomeArray = [self getAllIncomeByWallet:wallet];
    NSMutableArray *incomeArrayInDate = [[NSMutableArray alloc]init];
    for(Income *income in incomeArray){
        if (income.i_date.timeIntervalSince1970 >= dateFrom.timeIntervalSince1970
            &&income.i_date.timeIntervalSince1970 <=dateTo.timeIntervalSince1970){
            [incomeArrayInDate addObject:income];
        }
    }
    
    return incomeArrayInDate;
}

+(NSMutableArray*) getAllExpenseByDate:(Wallet *)wallet andDateFrom:(NSDate *)dateFrom andDateTo:(NSDate *)dateTo {
    
    NSMutableArray *expenseArray = [self getAllExpenseByWallet:wallet];
    NSMutableArray *expenseArrayInDate = [[NSMutableArray alloc]init];
    for(Expense *expense in expenseArray){
        if (expense.e_date.timeIntervalSince1970 >= dateFrom.timeIntervalSince1970
            && expense.e_date.timeIntervalSince1970 <= dateTo.timeIntervalSince1970){
            [expenseArrayInDate addObject:expense];
        }
    }
    return expenseArrayInDate;
}

+(NSMutableArray*)getAllExpenseByDate:(Wallet *)wallet andDate:(NSDate *)date {
    NSMutableArray *expenseArray = [self getAllExpenseByWallet:wallet];
    NSMutableArray *expenseArrayInDate = [[NSMutableArray alloc]init];
    NSLog(@"expense count %d", [expenseArray count]);
    for(Expense *expense in expenseArray){
        if (expense.e_date.timeIntervalSince1970 == date.timeIntervalSince1970){
            [expenseArrayInDate addObject:expense];
        }
    }
    return expenseArrayInDate;
}

+(NSMutableArray*)getAllIncomeByWallet:(Wallet *)wallet {
    NSArray *allPlans = [self getAllPlanInWallet:wallet];
    NSMutableArray *allIncomeInWallet = [[NSMutableArray alloc]init];
    for (Plan *plan in allPlans) {
        for (Income *income in [plan.pToIncome allObjects]) {
            [allIncomeInWallet addObject:income];
        }
    }
    NSLog(@"%d",[allIncomeInWallet count]);
    return  allIncomeInWallet;
}

+(NSMutableArray*)getAllExpenseByWallet:(Wallet *)wallet {
    NSArray *allPlans = [self getAllPlanInWallet:wallet];
    NSMutableArray *allExpenseInWallet = [[NSMutableArray alloc]init];
    for (Plan *plan in allPlans) {
        for (Expense *expense in [plan.pToExpense allObjects]) {
            [allExpenseInWallet addObject:expense];
        }
    }
    NSLog(@"Expense in Plan%d",[allExpenseInWallet count]);
    return  allExpenseInWallet;
}
//+(NSMutableDictionary *)
#pragma mark - ACTION

+(Plan *)planForName: (NSString *)pName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kPlan inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kPlanName, pName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    
    return [result objectAtIndex:0];
}

+(Plan *)planForName:(NSString *)pName andWallet:(Wallet *)wallet {
    
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    request.entity = [NSEntityDescription entityForName:kPlan inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"(%K= %@) AND (%K = %@)",kPlanName,pName,kPlanToWallet,wallet];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return [result objectAtIndex:0];
}

+(ExpenseType *)eTypeForName: (NSString *)tName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kExpenseType inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kExpenseTypeName, tName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    
    return [result objectAtIndex:0];
}

+(IncomeType *)iTypeForName: (NSString *)tName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kIncomeType inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kIncomeTypeName, tName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    
    return [result objectAtIndex:0];
}

+(Wallet *)walletForName: (NSString *)wName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kWallet inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kWalletName, wName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return [result objectAtIndex:0];
}

+(Debt *)debtForName: (NSString *)dName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kDebt inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kDebtName  , dName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return [result objectAtIndex:0];
}

+(Loan *)loanForName: (NSString *)lName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kLoan inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kLoanName  , lName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return [result objectAtIndex:0];
}
+(Income *)incomeForName: (NSString *)iName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kIncome inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kIncomeName  , iName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return [result objectAtIndex:0];

}
+(Expense *)expenseForName: (NSString *)eName
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:kExpense inManagedObjectContext:context];
    request.predicate = [NSPredicate predicateWithFormat:@"%K = %@", kExpenseName  , eName];
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    return [result objectAtIndex:0];
    

}

+(BOOL)movePlan: (NSString *)fromPlan toPlan: (NSString *)toPlan
{
    Plan *pFrom = [DataBaseAccess planForName:fromPlan];
    Plan *pTo = [DataBaseAccess planForName:toPlan];

    NSArray *incomes = [DataBaseAccess getAllIncomeInPlan:pFrom];
    NSArray *expenses = [DataBaseAccess getAllExpenseInPlan:pFrom];
    for(Income * income in incomes)
    {
        income.iToPlan = pTo;
    }
    for(Expense *expense in expenses)
    {
        expense.eToPlan = pTo;
    }
    
    return [DataBaseAccess saveData];
   
}

+(BOOL)movePlan:(NSString *)fromPlan toPlan:(NSString *)toNoPlan andWallet:(Wallet *)wallet {
    
    
    Plan *pFrom = [DataBaseAccess planForName:fromPlan];
    Plan *pTo = [DataBaseAccess planForName:toNoPlan andWallet:wallet];
    
    NSArray *incomes = [DataBaseAccess getAllIncomeInPlan:pFrom];
    NSArray *expenses = [DataBaseAccess getAllExpenseInPlan:pFrom];
    for(Income * income in incomes)
    {
        income.iToPlan = pTo;
    }
    for(Expense *expense in expenses)
    {
        expense.eToPlan = pTo;
    }
    
    [DataBaseAccess deletePlan:pFrom];
    return [DataBaseAccess saveData];
}
+(BOOL)moveIncomType: (NSString *)itFrom to: (NSString *)itTo
{
    IncomeType *itFromObj = [DataBaseAccess iTypeForName:itFrom];
    IncomeType *itToObjt = [DataBaseAccess iTypeForName:itTo];
    
    NSArray *incomes = [DataBaseAccess getAllIncomeInIncomeType:itFromObj];
    
    for(Income * income in incomes)
    {
        income.iToIncomeType = itToObjt;
    }
    
    return [DataBaseAccess saveData];
}

+(BOOL)moveExpenseType:(NSString *)etFrom to: (NSString *)etTo
{
    ExpenseType *etFromObj = [DataBaseAccess eTypeForName:etFrom];
    ExpenseType *etToObjt = [DataBaseAccess eTypeForName:etTo];
    
    NSArray *expenses = [DataBaseAccess getAllExpenseInExpenseType:etFromObj];
    
    for(Expense * expense in expenses)
    {
        expense.eToExpenseType = etToObjt;
    }
    
    return [DataBaseAccess saveData];

}


#pragma mark - CHECK
+(BOOL)checkExistPlan: (NSString *)pName
{
    id ix = [DataBaseAccess planForName:pName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistWallet: (NSString *)wName
{
    id ix = [DataBaseAccess walletForName:wName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistDebt: (NSString *)dName
{
    id ix = [DataBaseAccess debtForName:dName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistLoan: (NSString *)lName
{
    id ix = [DataBaseAccess loanForName:lName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistIncome: (NSString *)iName
{
    id ix = [DataBaseAccess incomeForName:iName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistExpense: (NSString *)eName
{
    id ix = [DataBaseAccess expenseForName:eName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistIncomeType: (NSString *)itName
{
    id ix = [DataBaseAccess iTypeForName:itName];
    if (ix) {
        return YES;
    }else return NO;
}

+(BOOL)checkExistExpenseType: (NSString *)etName
{
    id ix = [DataBaseAccess eTypeForName:etName];
    if (ix) {
        return YES;
    }else return NO;
}

//DELETE
#pragma mark - DELETE

+(void)deleteObject: (id)object
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    [context deleteObject:object];
    [context save:&error];

}

+(void)deleteArrayObject: (NSArray *)listObject
{
    folyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSError *error;
    for(id xxx in listObject)
    {
        [context deleteObject:xxx];
    }
    [context save:&error];
}

+(void)deleteDebtHistory:(DebtHistory *)hObject
{
    [DataBaseAccess deleteObject:hObject];
}
+(void)deleteLoanHistory:(LoanHistory *)hObject
{
    [DataBaseAccess deleteObject:hObject];
}

+(void)deleteDebt:(Debt *)dObject
{
    NSArray *listDebtHistory = [DataBaseAccess getAllDebtHistoryInDebt:dObject];
    for(id dh in listDebtHistory)
    {
        [DataBaseAccess deleteObject:dh];
    }
    [DataBaseAccess deleteObject:dObject];
}
+(void)deleteLoan:(Loan *)lObject
{
    NSArray *listLoanHistory = [DataBaseAccess getAllLoanHistoryInLoan:lObject];
    for(id lh in listLoanHistory)
    {
        [DataBaseAccess deleteObject:lh];
    }
    [DataBaseAccess deleteObject:lObject];
}

+(void)deleteIncome:(Income *)iObject
{
    [DataBaseAccess deleteObject:iObject];
}
+(void)deleteExpense:(Expense *)eObject
{
    [DataBaseAccess deleteObject:eObject];
}

+(void)deletePlan:(Plan *)pObject
{
    NSArray *listIncome = [DataBaseAccess getAllIncomeInPlan:pObject];
    for(id i in listIncome)
    {
        [DataBaseAccess deleteObject:i];
    }
    NSArray *listExpense = [DataBaseAccess getAllExpenseInPlan:pObject];
    for(id e in listExpense)
    {
        [DataBaseAccess deleteObject:e];
    }
    [DataBaseAccess deleteObject:pObject];
}

+(void)deleteWallet:(Wallet *)wObject
{
    NSLog(@"%@",wObject.w_name);
    NSArray *listDebt = [DataBaseAccess getAllDebtInWallet:wObject];
    for(id d in listDebt)
    {
        [DataBaseAccess deleteDebt:d];
    }
    NSArray *listLoan = [DataBaseAccess getAllLoanInWallet:wObject];
    for(id l in listLoan)
    {
        [DataBaseAccess deleteLoan:l];
    }
    NSArray *listPlan = [DataBaseAccess getAllPlanInWallet:wObject];
    for(id p in listPlan)
    {
        [DataBaseAccess deletePlan:p];
    }
    
    [DataBaseAccess deleteObject:wObject];
}

+(void)deleteIncomeType:(IncomeType *)itObject
{
    NSArray *listIncome = [DataBaseAccess getAllIncomeInIncomeType:itObject];
    for(id i in listIncome)
    {
        [DataBaseAccess deleteObject:i];
    }
    [DataBaseAccess deleteObject:itObject];
}

+(void)deleteExpenseType: (ExpenseType *)etObject
{
    NSArray *listExpenses = [DataBaseAccess getAllExpenseInExpenseType:etObject];
    for(id e in listExpenses)
    {
        [DataBaseAccess deleteObject:e];
    }
    [DataBaseAccess deleteObject:etObject];
}

@end
